<template>
  <div class="login">
    <div class="form">
     
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="0"
        size="medium"
      >
        <el-form-item label="" prop="phone">
          <el-input
            v-model.trim="ruleForm.phone"
            autocomplete="off"
            placeholder="请输入账号"
            @keyup.enter.native="submitForm('ruleForm')"
          >
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            type="password"
            v-model.trim="ruleForm.password"
            autocomplete="off"
            placeholder="请输入密码"
            @keyup.enter.native="submitForm('ruleForm')"
          >
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
           v-no-more-click:3000 
            class="submit"
            type="primary"
            @click="login('ruleForm')"
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { loginPost } from "@/api/login";
import { mapMutations } from "vuex";
import { validatePhone } from "./vaildate";
export default {
  name: "Login",
  data() {
    return {
      loading: false,

      ruleForm: {
        phone: "",
        password: "",
      },
      rules: {
        phone: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 32,
            message: "长度在 6 到 32 个字符",
            trigger: "blur",
          },
        ],
      },
      redirect: undefined,
      otherQuery: {},
    };
  },

  methods: {
    ...mapMutations({
      setToken: "setToken",
    }),
    login() {
        if (validatePhone(this.ruleForm.phone)) {
      let params = {
    
        phone: this.ruleForm.phone,
        password: this.ruleForm.password,
      };
      loginPost(params).then((res) => {
        let { code, data } = res;
        console.log(res);

        if (res.code == 200) {
          this.setToken(data);
          this.$cookies.set("token", data);
          if (this.$route.query.source) {
            this.$router.push({
              path: this.$route.query.source,
            });
          } else {
            this.$router.push({
              path: "/user",
            });
          }
        }
      });
    }
    }
  },
};
</script>
<style lang="less" scoped>
.login {
  display: flex;
  width: 100%;
  height: 100vh;
  min-height: 500px;
  overflow: auto;

  background-repeat: no-repeat;
  background-position: center center;
  background-attachment: fixed;
  background-size: cover;
  align-items: center;
  .form {
    flex: none;
    width: 320px;
    padding: 60px;
    margin: 0 auto;
    border-radius: 4px;
    background-color: #ffffff;
    .title {
      font-weight: 300;
      margin-top: 0;
      margin-bottom: 60px;
      text-align: center;
      color: #ffffff;
    }
    .submit {
      margin-top: 30px;
      width: 100%;
    }
  }
}
</style>
